package controllers;

import io.netty.channel.Channel;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.TextArea;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.InnerShadow;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import model.database.Function;
import model.client.request.SendToIndividualsRequestMessage;
import model.util.MessageVerticalBox;

import java.util.ArrayList;

/**
 * @author zjw
 * @date 2020/1/8 18:50
 */
public class ChatBoxInterfaceControllers {
    private ArrayList<String> ids = new ArrayList<>();
    private Stage primaryStage;
    private TextArea messageEditingTextArea;
    private ListView chatBoxListView;
    private Button sendOutButton;

    private String interfaceId;
    private Channel channel;

    public void setInterfaceId(String interfaceId) {
        this.interfaceId = interfaceId;
    }

    public void setChannel(Channel channel) {
        this.channel = channel;
    }

    public ChatBoxInterfaceControllers(ArrayList<String> ids, Stage primaryStage,
                                       TextArea messageEditingTextArea, ListView chatBoxListView) {
        this.ids.addAll(ids);
        this.primaryStage = primaryStage;
        this.messageEditingTextArea = messageEditingTextArea;
        this.chatBoxListView = chatBoxListView;
        this.sendOutButton = new Button("发送");
    }

    /**
     * 发送按钮上的控制器
     * @return 返回一个发送按钮
     */
    public Button getSendOutButton() {
        // 鼠标放上去时候的效果
        sendOutButton.addEventHandler(MouseEvent.MOUSE_ENTERED, (MouseEvent e) -> {
            sendOutButton.setEffect(new DropShadow());
        });

        // 鼠标离开时候的效果
        sendOutButton.addEventHandler(MouseEvent.MOUSE_EXITED, (MouseEvent e) -> {
            sendOutButton.setEffect(new InnerShadow());
        });

        /**
         * 发送信息按钮
         */
        sendOutButton.setOnAction(event -> {
            System.out.println("按下发送按钮");
            System.out.println("Channel : "+ channel);
            String message = messageEditingTextArea.getText();
            VBox myMessageVerticalBox = MessageVerticalBox.getMessageVerticalBox(ids.get(1), message, 1);
            chatBoxListView.getItems().add(myMessageVerticalBox);
            messageEditingTextArea.clear();
            String senderId = ids.get(1);
            String receiverId = ids.get(0);
            Function function = new Function();
            function.insertPrivateChatData(ids.get(1), ids.get(0), message);
            channel.writeAndFlush(new SendToIndividualsRequestMessage(senderId, receiverId, message, interfaceId));
        });
        return sendOutButton;
    }
}
